//
//  FollowViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/9/21.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "FollowViewController.h"

#import "TableViewDataSource.h"

#import "OfferSiteViewController.h"

#import "SecurityUtil.h"

#import "PopSelectionCell.h"
#import "FollowCell.h"

#import "LSPaoMaView.h"

static NSString *const followCellName = @"FollowCell";
static NSString *const SelectionCellName = @"SelectionCell";

//the height of the cell
#define kCellHeight 100;

#define kRollViewHeight 50 / 2

//新添：遵守DataSource协议
@interface FollowViewController () <UITableViewDelegate, UITableViewDataSource>

{
    WYPopoverController *popover;
    UITableViewController *tbc;
    NSArray *selectionArr;
    NSMutableArray *sourceData;
}

@property (nonatomic, strong) TableViewDataSource *dataSource;

@end

@implementation FollowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%ld", self.tabBarController.selectedIndex);
    [self creatUI];
    [self customNavBar];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Custom User Interface

- (void)creatUI{
    [self fakeValue];
    [self setupTableView];

}

- (void)fakeValue{
    selectionArr = @[@"搜索" , @"消息"];
    sourceData = [[NSMutableArray alloc] initWithCapacity:30];
    for (int i = 0; i < 30; i++) {
        HomePageModel *model = [[HomePageModel alloc] init];
        model.itemImage = [UIImage imageNamed:@"bg.jpg"];
        model.title = [NSString stringWithFormat:@"第%d个title第%d个title第%d个title第%d个title第%d个title第%d个title第%d个title第%d个title", i, i, i, i, i, i, i, i];
        model.state = arc4random() % 3 + 1;
        model.price = [NSString stringWithFormat:@"%.2f", (arc4random() % 500000) / 100.00 + 20000];
        model.starring = [NSString stringWithFormat:@"%d", (arc4random() % 3000)];
        
        int a = arc4random() % 30000 + 30;
        
        NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:a];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = timeFormatter;
        model.boundTime = [formatter stringFromDate:date];
        
        [sourceData addObject:model];
    }
}

- (void)customNavBar{
    
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"更多"] style:UIBarButtonItemStyleDone target:self  action:@selector(bbiClick:)];
    bbi.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = bbi;
    
}

- (void)setupTableView{
    
    LSPaoMaView *view = [[LSPaoMaView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, UISCREENWIDTH, kRollViewHeight) title:@"一条大河波浪宽，风吹稻花向两岸，两岸春色管不住，一直红杏出墙来，墙外有墙楼外楼，楼上一直小白狐，狐狸脑袋大如斗，斗牛就看今下午"];
    [self.view addSubview:view];
    
    TableViewCellConfigurBlock cellConfigureBlock = ^(FollowCell *cell, HomePageModel *model){
        [cell configureCellWithModel:model];
    };
    _dataSource = [[TableViewDataSource alloc] initWithItems:sourceData cellIdentifier:followCellName configureBlock:cellConfigureBlock];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kRollViewHeight, UISCREENWIDTH, UISCREENHEIGHT - kRollViewHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = _dataSource;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [_tableView registerClass:[FollowCell class] forCellReuseIdentifier:followCellName];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    }];
    
    [self.view bringSubviewToFront:view];
}

#pragma mark - TableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 1) {
        return 50.0f;
    }else{
        return kCellHeight;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 1) {
        [popover dismissPopoverAnimated:NO];
        //TODO: add the selection block here
    }else{
        OfferSiteViewController *osvc = [[OfferSiteViewController alloc] init];
        [self.navigationController pushViewController:osvc animated:NO];
    }
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"取消关注" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [sourceData removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }];
    rowAction.backgroundColor = RGB_COLOR(@"#d40000");
    [array addObject:rowAction];
    return array;
}

#pragma mark - UITableView DataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PopSelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:SelectionCellName forIndexPath:indexPath];
    if (!cell) {
        cell = [[PopSelectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SelectionCellName];
    }
    [cell configureCellWithModel:selectionArr[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return selectionArr.count;
}

#pragma mark - BarButtonItem Action

- (void)bbiClick:(UIBarButtonItem *)bbi{
    
    tbc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    tbc.tableView.tag = 1;
    
//    TableViewCellConfigurBlock selectionCellConfigureBlock = ^(PopSelectionCell *cell, NSString *text){
//        [cell configureCellWithModel:text];
//    };
//    
//    TableViewDataSource *selectionDataSource = [[TableViewDataSource alloc] initWithItems:[selectionArr copy] cellIdentifier:SelectionCellName configureBlock:selectionCellConfigureBlock];
    
    tbc.tableView.dataSource = self;
    tbc.tableView.delegate = self;
    
    tbc.tableView.backgroundColor = [UIColor darkGrayColor];
    tbc.tableView.scrollEnabled = NO;
    tbc.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tbc.preferredContentSize = CGSizeMake(100, 100);
    [tbc.tableView registerClass:[PopSelectionCell class] forCellReuseIdentifier:SelectionCellName];
    
    //Popover appearance
    WYPopoverBackgroundView *appearance = [WYPopoverBackgroundView appearance];
    [appearance setInnerCornerRadius:15];
    [appearance setOuterCornerRadius:15];
    [appearance setOuterStrokeColor:[UIColor clearColor]];
    [appearance setOuterShadowOffset:CGSizeZero];
    [appearance setFillTopColor:[UIColor clearColor]];
    [appearance setTintColor:[UIColor clearColor]];
    [appearance setViewContentInsets:UIEdgeInsetsZero];
    [appearance setBorderWidth:0];
    
    popover = [[WYPopoverController alloc] initWithContentViewController:tbc];
    [popover presentPopoverFromBarButtonItem:bbi permittedArrowDirections:WYPopoverArrowDirectionNone animated:YES];
}

@end
